package com.dms.servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.dms.bean.Announce;
import com.dms.bean.Student;
import com.dms.dao.AnnounceDao;

/**
 * Servlet implementation class IndexServlet
 */
@WebServlet("/IndexServlet")
public class IndexServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public IndexServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		this.doPost(request, response);
		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		 response.setContentType("text/html");
		 response.setCharacterEncoding("UTF-8");
	   	 request.setCharacterEncoding("UTF-8");
	 	HttpSession session = request.getSession();
		Student stu = (Student) session.getAttribute("student");
		String action = request.getParameter("act");
		if (action == null)
			action = "";
		if (action.equals("index")) {
			this.index(request,response,stu);
		}
		if (action.equals("info")) {
			this.info(request, response,stu);
		}
		
		
	}
	protected void index(HttpServletRequest request, HttpServletResponse response,Student stu) throws ServletException, IOException {
		List<Announce> announces = AnnounceDao.list();
		request.setAttribute("list", announces);
		request.getRequestDispatcher("announce.jsp").forward(request, response);
	}
	protected void info(HttpServletRequest request, HttpServletResponse response,Student stu) throws ServletException, IOException {
		int id = Integer.parseInt(request.getParameter("AnnID"));
		Announce announce = AnnounceDao.find(id);
		System.out.print(announce.getContent());
		request.setAttribute("ann", announce);
		request.getRequestDispatcher("info.jsp").forward(request, response);
	}
}
